/**
 * vmshell - An very minimal shell
 *
 * Author: Renê de Souza Pinto
 * Date: Mar, 2009
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#define MAXBUFFER 1024

extern char **environ;

int main(int argc, char **argv)
{
        int status;
        pid_t child;
        char line[MAXBUFFER];
        char stop = 0;

        /**
         * An very simple loop to do two things:
         *
         *      1) Read the user command line
         *      2) Check for exit command or fork proccess
         */
        while(!stop) {
                printf("minimalshell> ");
                fgets(line, MAXBUFFER, stdin);
                line[strlen(line) - 1] = '\0'; /* drop \n */

                if( strncmp(line, "exit", 4) == 0) {
                        stop = 1;
                } else {
                        /* fork proccess */
                        if( (child = fork()) ) {
                                /**
                                 * This is the father (shell), so wait for
                                 * child termination
                                 */
                                waitpid(child, &status, 0);
                        } else {
                                /**
                                 * This is the child, execute the command
                                 */
                                execve(line, NULL, environ);

                                /* Something was wrong, show error message */
                                printf("\nCannot execute: %s\n", line);
                        }
                }
        }

        return(0);
}

